/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        /*unordered_map<ListNode*,bool>mp;
        ListNode* temp=headA;
        while(temp!=nullptr){
            mp[temp]=1;
            temp=temp->next;
        }
        temp=headB;
        while(temp!=nullptr){
            if(mp[temp])return temp;
            temp=temp->next;
        }
        return nullptr;*/
        ListNode* pA = headA, * pB = headB;
        while (pA != pB) {
            pA = pA == nullptr ? headB : pA->next;
            pB = pB == nullptr ? headA : pB->next;
        }
        return pA;
    }
};
